package com.neusoft.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.neusoft.bean.AdminInfoBean;
import com.neusoft.service.IAdminInfoService;

// springmvc使用@RestController支持rest请求解析
@RestController
public class RestfulAction {
	/*
	 * 注解方法：进行service层的实体bean的依赖注入
	 * 采用了面向接口的编程思想：在service层添加一个接口层，在调用service时，使用service接口类型的变量
	 */
	@Resource
	private IAdminInfoService adminS;
	@Resource
	HttpServletRequest request;

	@RequestMapping(value = "/adminInfo/{id}", method = RequestMethod.GET)
	public ResponseEntity<AdminInfoBean> doRestGetId(@PathVariable("id") Integer uid) {
		// 查询结果
		AdminInfoBean adminInfo = adminS.selectByPrimaryKey(uid);
		// 使用responseentity返回json字符串
		return new ResponseEntity<AdminInfoBean>(adminInfo, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/adminInfo/", method = RequestMethod.GET)
	public ResponseEntity<List<AdminInfoBean>> doRestGetAll() {
		// 查询结果
		List<AdminInfoBean> lstAdmiInfo = adminS.getAllAdminInfo(0);
		// 使用responseentity返回json字符串
		return new ResponseEntity<List<AdminInfoBean>>(lstAdmiInfo, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/adminInfo/", method = RequestMethod.POST)
	public ResponseEntity<Void> doRestPostSave(@RequestBody AdminInfoBean adminB,UriComponentsBuilder
			ucBuilder) {
		// 打印要保存的实体bean的内容
		System.out.println("rest-save:"+adminB.toString());
		// TODO 保存数据
		//adminS.saveOrUpdateAdminInfo(adminB);
		
		// 保存完成之后，倒序查询所有的结果进行显示
		//List<AdminInfoBean> lstAdmiInfo = adminS.getAllAdminInfo(0);
		// 使用responseentity返回json字符串
		//return new ResponseEntity<List<AdminInfoBean>>(lstAdmiInfo, HttpStatus.OK);
		// 请求的head部分，包含请求目标的uri
		HttpHeaders headers = new HttpHeaders();
		// 设置request请求的header的location属性，创建一个新的请求
		headers.setLocation(ucBuilder.path("/adminInfo/").build().toUri());
		//headers.setLocation(ucBuilder.path("/adminInfo/{id}").buildAndExpand(1).toUri());
		return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "/adminInfo/{id}", method = RequestMethod.PUT)
	public ResponseEntity<AdminInfoBean> doRestPutId(@PathVariable("id") Integer uid,@RequestBody AdminInfoBean adminB) {
		// 修改操作
		// 如果发送的json数据中没有id属性
		//adminB.setAdminid(uid);
		adminS.saveOrUpdateAdminInfo(adminB);
		// 将修改之后的数据重新查询获取		
		AdminInfoBean adminInfo = adminS.selectByPrimaryKey(uid);
		// 使用responseentity返回json字符串
		return new ResponseEntity<AdminInfoBean>(adminInfo, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/adminInfo/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<String> doRestDeleteId(@PathVariable("id") Integer uid) {
		// 删除操作
		adminS.deleteByPrimaryKey(uid);
		// 使用responseentity返回json字符串
		return new ResponseEntity<String>("删除成功", HttpStatus.OK);
	}
}
